package com.ziyun.pop.web.modules.capital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.ucenter.api.VerifyCodeService;
import com.service.ucenter.constant.UcenterResult;
import com.ziyun.pop.common.e.capital.ApplyStatusEnum;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.capital.entity.MerchantApplyCashEntity;
import com.ziyun.pop.modules.capital.entity.MerchantApplyCashItemsEntity;
import com.ziyun.pop.modules.capital.entity.MerchantApplyLogEntity;
import com.ziyun.pop.modules.capital.service.MerchantApplyCashItemsService;
import com.ziyun.pop.modules.capital.service.MerchantApplyCashService;
import com.ziyun.pop.modules.capital.service.MerchantApplyLogService;
import io.renren.common.exception.ResultCode;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zh
 * @email 296588519@qq.com
 * @date 2018-08-03 10:29:41
 */
@RestController
@RequestMapping("capital/merchantapplycash")
public class MerchantApplyCashController {
	@Autowired
	private MerchantApplyCashService merchantApplyCashService;

	@Autowired
	private MerchantApplyCashItemsService merchantApplyCashItemsService;

	@Autowired
	private MerchantApplyLogService merchantApplyLogService;

	@Autowired
	private VerifyCodeService verifyCodeService;




	@RequestMapping("/applyCashLog")
	// @RequiresPermissions("capital:merchantapplycash:list")
	public R applyCashLog(@RequestBody Map<String, Object> params) {
		SysUserEntity user =  UserUtils.getCurentUser();
		params.put("merchantType", user.getMerchantType());// 存放用户类型
		params.put("sellerId", user.getSellerId());// 存放用户id
		PageUtils page =  this.merchantApplyCashService.queryPage(params);
		// 枚举类转换
		Map<String,Object> userData = new HashMap<>();
		userData.put("applyStatusEnum", JsonUtils.enumToList(ApplyStatusEnum.class));
		return R.ok().put("page", page).put("userData",userData);
	}


	/**
	 * 申请提现
	 * @return
	 */
	@RequestMapping("/applyCash")
	public R applyCash(@RequestBody Map<String, Object> params){
		SysUserEntity user =  UserUtils.getCurentUser();
		params.put("merchantType", user.getMerchantType());// 存放用户类型
		params.put("sellerId", user.getSellerId());
		params.put("userName", user.getUsername());
		this.merchantApplyCashService.applyCash(params);
		return R.ok();
	}

	/**
	 * 小b审核提现列表
	 */
	@RequestMapping("/smallBCheckList")
	// @RequiresPermissions("capital:merchantapplycash:list")
	public R smallBCheckList(@RequestParam Map<String, Object> params) {

		PageUtils page = merchantApplyCashService.queryPage(params);
		return R.ok().put("page", page);
	}



	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	// @RequiresPermissions("capital:merchantapplycash:info")
	public R info(@PathVariable("id") Integer id) {
		// 请求主表信息
		MerchantApplyCashEntity merchantApplyCash = merchantApplyCashService.getById(id);

		// 请求明细
		List<MerchantApplyCashItemsEntity> merchantApplyCashItemsList = merchantApplyCashItemsService.selectApplyCashItemList(id);

		// 请求日志
		List<MerchantApplyLogEntity> merchantApplyLogList = merchantApplyLogService
				.list(new QueryWrapper<MerchantApplyLogEntity>().eq("apply_cash_id", id));

		R r = R.ok();
		r.put("merchantApplyCash", merchantApplyCash);
		r.put("merchantApplyCashItems", merchantApplyCashItemsList);
		r.put("merchantApplyLog", merchantApplyLogList);

		return r;
	}


	/**
	 * 发送短信
	 */
	@RequestMapping("/sendMsg")
	public R sendMsg(@RequestBody String mobile) {
		UcenterResult ucenterResult = this.verifyCodeService.getVerifyCode(mobile);
		if (ucenterResult.getCode() == ResultCode.SUCCESS.getCode()){
			return R.ok(ucenterResult.getMessage());
		}else {
			return R.error(ucenterResult.getCode(),ucenterResult.getMessage());
		}
	}

	/**
	 * 验证短信
	 */
	@RequestMapping("/checkMsg")
	public R checkMsg(@RequestBody Map<String,Object> msgCheck) {
		String mobile = msgCheck.get("mobile").toString();
		String verifyCode = msgCheck.get("verifyCode").toString();
		UcenterResult ucenterResult = this.verifyCodeService.checkVerifyCode(mobile,verifyCode);
		if (ucenterResult.getCode() == ResultCode.SUCCESS.getCode()){
			return R.ok(ucenterResult.getMessage());
		}else {
			return R.error(ucenterResult.getCode(),ucenterResult.getMessage());
		}
	}


}
